//	ID : pallab81
// 	PROG : 10130
// 	LANG : C++
// 	2011-07-16-21.48.32 Saturday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

const int inf_n = (int)1010 ;
const int inf_g = (int)110;
int N; // number of objects
int P[inf_n];
int W[inf_n];

int G; // number of people in my group
int MW[ inf_g ];
int dp[ inf_n ][ (int)30+20 ];

inline void read() {
    scanf("%d",&N);
    fo(i,N) {
        scanf("%d %d",&P[i],&W[i]);
    }
    scanf("%d",&G);
    fo(i,G) {
        scanf("%d",&MW[i]);
    }
}

inline int go(int now_item, int now_weight) {
    if( now_item==N ) {
        return 0;
    }
    int &best = dp[ now_item ][ now_weight ];
    if( best!=-1 )return best;

    if( W[now_item ] > now_weight ) {
        return go( now_item+1, now_weight );
    } else {
        int one = go(now_item+1, now_weight); //na niye
        int two = P[now_item] + go(now_item+1, now_weight-W[now_item] ); //niye
        best = max(one,two);
    }
    return best;
}
inline void proc() {
    int res=0;
    fo(i,G) {
        SET(dp,-1);
        int var = go(0,MW[i]);
        res+=var;
    }
    printf("%d\n",res);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","rt",stdin);
#endif
    int cases;
    for(scanf("%d",&cases); cases; --cases) {
        read();
        proc();
    }

    return 0;
}


